<template>
  <div class="novel-read-background">
    <div class="novel-reading">
      <div class="novel-reading-top">
        <el-icon class="novel-reading-top-icon" size="30" @click="goBack"
          ><ArrowLeft
        /></el-icon>
        <el-divider class="novel-reading-top-divider" direction="vertical" />
        <span class="novel-reading-title">{{ novel.title }}</span>
      </div>
      <el-divider />
      <div class="novel-reading-content">
        <div class="novel-reading-content-title">
          第{{ novel.chapterNum }}章 {{ novel.chapterName }}
        </div>
        <div
          class="novel-reading-content-text"
          v-for="(item, index) in novel.chapterContext"
          :key="index"
        >
          <!-- <el-text class="novel-reading-content-text-item">
            {{ item }}
          </el-text> -->

          <p class="novel-reading-content-text-item">
            {{ item }}
          </p>
        </div>
        <div class="novel-reading-content-footer">
          <el-button
            v-if="currentChapterIndex != 0"
            class="novel-reading-content-footer-last"
            type="success"
            color="#fc9f0a56"
            @click="lastChapter"
            round
            >上一章</el-button
          >
          <el-button
            v-if="currentChapterIndex != chapterList.length-1"
            class="novel-reading-content-footer-next"
            type="success"
            color="#fc9f0a56"
            @click="nextChapter"
            round
            >下一章</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getNovelChapterContent,
  getNovelChapterList,
} from "@/api/novelReading";

export default {
  data() {
    return {
      currentChapterIndex: 0,
      chapterList: [],
      novel: {
        title: "",
        chapterNum: 1,
        chapterName: "",
        chapterContext: [],
      },
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    lastChapter() {
      this.currentChapterIndex--;
      this.getNovelChapter();
    },
    nextChapter() {
      this.currentChapterIndex++;
      this.getNovelChapter();
    },

    async getNovelChapter() {
      const res = await getNovelChapterContent(
        this.chapterList[this.currentChapterIndex].id
      );
      if (res.data.success) {
        this.novel = res.data.result;
      }
    },
    async getChapterList() {
      const res = await getNovelChapterList(this.$route.params.novelId,this.$route.params.chapterId);
      if (res.data.success) {
        this.currentChapterIndex = res.data.result.currentChapterIndex;
        this.chapterList = res.data.result.chapterList;
        this.getNovelChapter();
      }
    },
  },
  created() {
    this.getChapterList();
  },
};
</script>

<style>
.novel-read-background {
  display: grid;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-color: #e0e0e0;
}
.novel-reading {
  width: 1000px;
  background-color: #f2f2f2;
}
.novel-reading-top {
  height: 40px;
  margin: 20px 60px;
}
.novel-reading-top-divider {
  width: 10px;
}
.novel-reading-top-icon {
  position: relative;
  top: 9px;
}
.novel-reading-title {
  font-size: large;
}
.novel-reading-content-title {
  position: relative;
  left: 100px;
  margin: 25px 10px;
  font-size: 25px;
}
.novel-reading-content-text {
  margin: 10px 100px;
  font-size: 1.6rem;
}
.novel-reading-content-text-item {
  line-height: 2.5rem;
  text-indent: 30px;
  font-size: medium;
}
.novel-reading-content-footer {
  display: flex;
  justify-content: center;
  margin: 50px 0;
}
.novel-reading-content-footer-last {
  position: relative;
  right: 30px;
  width: 150px;
}
.novel-reading-content-footer-next {
  position: relative;
  left: 30px;
  width: 150px;
  color: #fc9f0a56;
}
</style>